//
//  CollectJobberVController.m
//  Experss
//
//  Created by 嗯，大葱 on 17/8/7.
//  Copyright © 2017年 嗯，大葱. All rights reserved.
//

#import "CollectJobberVController.h"
#import "CollectJobberCell.h"
#import "CollectShopModel.h"
#import "InStoreController.h"



@interface CollectJobberVController ()<UITableViewDelegate ,UITableViewDataSource>

@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *dataArray;

@end

@implementation CollectJobberVController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self configureGoodView];
    _dataArray = [NSMutableArray array];
    [self getcollectioShopData];
}

//获取收藏批发商列表
- (void)getcollectioShopData {
    NSDictionary *saveDic = [SaveDataMethord readLoginData];
    NSDictionary *parameDic = @{@"userid":saveDic[@"userid"],@"actoken":[Md5Apiway getMD5data:saveDic[@"userid"]]};
    
    [HttpRequest getWithURLString:POST(Shop_PersonalGetShopFavorite) parameters:parameDic success:^(id responseObject) {
        if (_dataArray) {
            [_dataArray removeAllObjects];
        }
        
        if ([responseObject[@"ret"] intValue] != 200) {
            [MBProgressHUD showText:@"获取关注商家出错"];
            return ;
        }
        NSDictionary *tempD = responseObject[@"data"];
        
        if (![[NSString stringWithFormat:@"%@",tempD[@"code"]] isEqualToString:@"0"]) {
            [MBProgressHUD showText:tempD[@"msg"]];
            return;
        }
        
        _dataArray =[CollectShopModel mj_objectArrayWithKeyValuesArray:tempD[@"list"]];
        
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        
    }];

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)configureGoodView {
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, kScreen_Height-64 -44-10) style:(UITableViewStylePlain)];
    
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.separatorStyle = kSpNone;
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:_tableView];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"CollectJobberCell" bundle:nil] forCellReuseIdentifier:@"CollectJobberCell"];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 80;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view= [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, 10)];
    view.backgroundColor = KShouYeBg;
    return view;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CollectJobberCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CollectJobberCell"];
    cell.selectionStyle = kSeNone;
    cell.cencleBtn.layer.borderWidth = 1.0;
    cell.cencleBtn.layer.borderColor = [KShouYeBg CGColor];
    
    CollectShopModel *model = self.dataArray[indexPath.row];
    
    [cell.imageV sd_setImageWithURL:[NSURL URLWithString:model.logo] placeholderImage:[UIImage imageNamed:@"quetu"]];
    cell.nameLB.text = model.shop_name;
    
    
    cell.guanZhuLB.text = [NSString stringWithFormat:@"%@ 关注",model.Favorite];
    
    [cell.cencleBtn addTarget:self action:@selector(handleCencleCollectShops:) forControlEvents:(UIControlEventTouchUpInside)];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    CollectShopModel *model = self.dataArray[indexPath.row];
    InStoreController *str = [[InStoreController alloc] init];
    str.shopID =model.shop_id;
    [self.navigationController pushViewController:str animated:YES];
}


//取消关注商家
- (void)handleCencleCollectShops :(UIButton *)sender {
    CollectJobberCell *cell = (CollectJobberCell *)[[sender superview] superview];
    NSIndexPath *indePath = [self.tableView indexPathForCell:cell];
    CollectShopModel *model = self.dataArray[indePath.row];
    
    NSDictionary *saveDic = [SaveDataMethord readLoginData];
    NSDictionary *parameDic = @{@"userid":saveDic[@"userid"],@"shop_id":model.shop_id,@"actoken":[Md5Apiway getMD5data:saveDic[@"userid"]]};
    
    
    [HttpRequest postWithURLString:POST(Shop_MemberEditShopFavorite) parameters:parameDic success:^(id responseObject) {
        
        if ([responseObject[@"ret"] intValue] != 200) {
            [[DSLHUD shareDSLHUD] showTextNoActivity:@"操作失败" timeLength:1.5];
            return ;
        }
        [self.view showTextNoActivity:responseObject[@"data"][@"msg"] timeLength:1.5];
        [self getcollectioShopData];
        
    } failure:^(NSError *error) {
        
    }];
    
    
}
@end
